package com.qiyou.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import com.qiyou.dao.IRemindInfoDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.RemindInfo;
import com.qiyou.util.LogUtil;

/**
 * 个人提醒信息
 * 
 * @author 杨强
 */
@WebServlet("/RemindListService")
public class RemindListService extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Logger log = LogUtil
            .getLogger(RemindListService.class);

    public RemindListService() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");

        String userID = request.getParameter("id");
        IRemindInfoDao dao = DaoFactory.getRemindInfoDao();
        List<RemindInfo> remindInfos = null;
        try {
            remindInfos = dao.getRemindInfos(userID);
        } catch (SQLException e) {
            log.error(e);
        }
        JSONArray questionjson = new JSONArray();
        if (remindInfos != null && !remindInfos.isEmpty()) {
            for (RemindInfo remindInfo : remindInfos) {
                questionjson.put(new JSONObject(remindInfo));
            }
        }
        String msg = questionjson.toString();
        PrintWriter out = response.getWriter();
        out.print(msg);
        out.flush();
        out.close();

        log.info("用户:" + userID + ",访问了个人提醒信息:" + msg);
    }

}
